package com.examples.program;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Driver {

	public static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	public static final String DB_URL = "jdbc:sqlserver://localhost;databasename=EMP";
	public static final String DB_USERNAME = "sa";
	public static final String DB_PASSWORD = "123";

	static {
		try {
			Class.forName(JDBC_DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Connection connection = null;
		CallableStatement statement = null;
		try {
			connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);	
			final String sqlCmd = "{call SP_Employees_GetAll()}";
			statement = connection.prepareCall(sqlCmd);
			ResultSet resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				int age = resultSet.getInt("age");
				String first = resultSet.getString("first");
				String last = resultSet.getString("last");
				System.out.println("ID : " + id);
				System.out.println("Age : " + age);
				System.out.println("FirstName : " + first);
				System.out.println("LastName : " + last);
				System.out.println("-------------------------------");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (statement != null) statement.close();
				if (connection != null) connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}
}
